import { _decorator, Component, Node, Label, Sprite, Button, randomRange, Vec2, Prefab, instantiate } from 'cc';
import { DisObjUtil } from './DisObjUtil';
const { ccclass, property } = _decorator;

@ccclass('Utilitys')
export class Utilitys extends Component {
    /**
     * 单类
     */
    public static Instance:Utilitys = null;

    onLoad(){
        if(null == Utilitys.Instance){
            Utilitys.Instance = this;
        }else{
            this.destroy();
            return;
        }
    }

    /**
     * 为子元素 childName Label 设置文本
     * Label.string = labelStr;
     * @param node 
     * @param childName 
     * @param labelStr 
     * @returns 
     */
    public setChildLabelString(node:Node, childName:string, labelStr:string):void{
        //
        if(!this.isValidNode(node)) return;
        //
        var child = node.getChildByName(childName);
        if(!this.isValidNode(child)) return;
        //
        var lbl = child.getComponent(Label);
        if(null == lbl) return;
        //
        lbl.string = labelStr;
    }

    /**
     * 设置 node 子节点隐藏状态
     * @param node 
     * @param childName 
     * @param isActive 
     * @returns 
     */
    public setChildNodeActive(node:Node, childName:string, isActive:boolean):void{
        //
        if(!this.isValidNode(node)) return;
        //
        var child = node.getChildByName(childName);
        if(!this.isValidNode(child)) return;
        //
        child.active = isActive;
    }

    /**
     * 设置子元素 childName 显示｜隐藏 状态
     * @param node 
     * @param childName 
     * @param activeState 
     */
    public setChildActivie(node:Node, childName:string, activeState:boolean):void{
        //
        if(!this.isValidNode(node)) return ;
        //
        var childNode = node.getChildByName(childName);
        if(null == childNode) return;
        //
        childNode.active = activeState;
    }
    /**
     * 判断 node 节点是否有效
     * @param node 
     * @returns 
     */
    public isValidNode(node:Node):boolean{
        return DisObjUtil.isValid(node);
    }
    /**
     * 
     * @param node 
     * @returns 
     */
    public destoryNode(node:Node):void{
        DisObjUtil.removeMe(node);
    }

    /**
     * 随机区间 minValue <= x < maxValue，区间的整数
     * @param minValue 
     * @param maxValue 
     */
    public randomRangeInt(minValue:number, maxValue:number):number{
        return Math.floor(randomRange(minValue, maxValue));
    }

    /**
     * 延迟
     * @param fn 
     * @param delayMillTs 
     * @param args 
     */
    public setTimeoutFn(fn:Function, delayMillTs:number, ...args):number{
        return setTimeout(fn, delayMillTs, args);
    }
    
    /**
     * 
     * @param delayId 
     */
    public clearTimeoutId(delayId:number):void{
        clearTimeout(delayId);
    }

    /**
     * 为 按钮绑定 click 事件
     * @param btn 
     * @param fn 
     * @param target 
     */
    public bindBtnClick(btn:Button, fn:Function, target:any){
        btn.node.on(Button.EventType.CLICK, fn, target);
    }

    /**
     * 字符串到数字的转换：
     * @param value  要分析的字符串
     * @param radix 基数(数学数字系统中的基数，例如十进制为10，二进制为2)
     * @returns 
     */
    public stringToInt(value:string, radix:number=10):number{
        return parseInt(value, radix);
    }
    /**
     * 
     * @param value 
     * @param fractionDigits 
     * @returns 
     */
    public stringToFloat(value:string, fractionDigits:number=0):number{
        return Number(parseFloat(value).toFixed(fractionDigits));
    }
    /**
     * 
     * @param value 
     * @param fractionDigits 
     * @returns 
     */
    public stringToFloatStr(value:string, fractionDigits:number=0):string{
        return parseFloat(value).toFixed(fractionDigits);
    }

    /**
     * 设置 node 位置，需要注意以下事宜：
     * 1.检查 node 是否已经添加 “widget”
     * @param node 
     * @param px 
     * @param py 
     */
    public setPos(node:Node, px:number, py:number):void{
        node.setPosition(px, py);
    }

    /**
     * 设置 node 缩放
     * @param node 
     * @param value 
     */
    public setScale(node:Node, value:number):void{
        node.setScale(value, value);
    }

    /**
     * 创建预制
     * @param prefab 
     * @returns 
     */
    public instantiate(prefab:any):any{
        var prefabNode = instantiate(prefab);
        return prefabNode;
    }

}

